package android.preference;

import com.sukumar.android.sms.crypto.Cryptoutility;

import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;

public class EditTextPrefPassword extends EditTextPreference {

	public EditTextPrefPassword(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public EditTextPrefPassword(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public EditTextPrefPassword(Context context) {
		super(context);
	}

	@Override
	public void setText(String text) {
		String encText = "";
		boolean enc = false;
		if (text != null && !text.equals("")) {
			try {
				encText = Cryptoutility.decrypt(text);
				enc = true;
				super.setText(text);
			} catch (Exception e) {
				e.printStackTrace(); 
			}
			if(!enc){
				try {
					encText = Cryptoutility.encrypt(text);
					super.setText(encText);

				} catch (Exception e1) {
					e1.printStackTrace();
				}				
			}
		}

	}

	@Override
	public String getText() {
		String encText = super.getText();
		String retText = "";
		if (encText != null && !encText.equals("")) {
			try {
				retText = Cryptoutility.decrypt(encText);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return retText;
	}

}
